var express = require('express');
var router = express.Router();
const multer = require('multer');
const path = require('path');
const position_add_c = require('../controller/position_add_c.js')


//图片上传和保存在本地后端服务器
var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, path.resolve(__dirname, '../public/upload'))
    },
    filename: function(req, file, cb) {
        let fileStr = file.originalname //3.jpg 
        let docIndex = file.originalname.indexOf('.') //1
        let fileEnd = fileStr.substr(docIndex) //.jpg

        let fileName = file.fieldname + '-' + Date.now() + fileEnd
        req.fileName = fileName
        cb(null, fileName)
    }
})

var upload = multer({ storage: storage })


/* GET users listing. */
router.route('/add')
    .post(upload.single('companyLogo'), (req, res, next) => {
        position_add_c.add({ req, res, next }) //对象，按需
    })

.get(function(req, res, next) {
        res.send('get')
    })
    //express约定的视图文件夹views  所以res.render(路径直接写文件夹名)
    //res.render({options}) 去向模板中渲染数据, 可以把视图响应给客户端.

router.route('/find')
    .get((req, res, next) => {
        position_add_c.find({ req, res, next })
    });

router.route('/remove')
    .delete((req, res, next) => {
        position_add_c.remove({ req, res, next })
    })
module.exports = router;